package cst.start.contactmanage311zrb;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import java.util.ArrayList;
import java.util.List;

public class ContactDBAdapter {

    final static int VERSON = 1;
    private final static String DBNAME = "contact.db";
    private final static String TABLE = "contact";
    private final static String ID="_id";
    private final static String NAME="name";
    private final static String TEL="tel";
    private final static String TYPE="type";
    private final static String GRO="gro";
    private Context context;
    private OpenHelper openHelper;
    private SQLiteDatabase database;
    private static ContactDBAdapter dbAdapter;

    public static ContactDBAdapter getInstance(Context context){
        if(dbAdapter==null){
            dbAdapter=new ContactDBAdapter(context);

        }
        return dbAdapter;
    }

    private ContactDBAdapter(Context context) {
        this.context = context;
        openHelper = new OpenHelper(context);
    }

    public void openDB() {
        if (database == null) {
            database = openHelper.getWritableDatabase();
        }
    }

    public void closeDB() {
        //关闭数据库
        if(database!=null){
            database.close();
            database=null;
        }
    }

    public long insert(ContactBean contactBean) {
        long id=-1 ;
        //
        //插入一条记录，返回值是插入条目的id号，如果插入成功，返回值不能是-1的
        //
        ContentValues contentValues = new ContentValues();
        contentValues.put(NAME,contactBean.getName());
        contentValues.put(TEL,contactBean.getTel());
        contentValues.put(TYPE,contactBean.getType());
        contentValues.put(GRO,contactBean.getGro());

        id=database.insert(TABLE,null,contentValues);
        return  id;
    }

    public void update(ContactBean contactBean){
        //根据指定的contactbean对象的id号，更新该条记录
        ContentValues contentValues = new ContentValues();
        contentValues.put(ID,contactBean.get_id());
        contentValues.put(NAME,contactBean.getName());
        contentValues.put(TEL,contactBean.getTel());
        contentValues.put(TYPE,contactBean.getType());
        contentValues.put(GRO,contactBean.getGro());

        //String[] whereArges={Integer.toString(contactBean.get_id())};
        String[] whereArges={String.valueOf(contactBean.get_id())};
        database.update(TABLE,contentValues,ID+"=?",whereArges);
    }

    public int delete(ContactBean contactBean){
        int cnt=0;
        String[] whereArgs=new String[]{ contactBean.getTel() };
        cnt=database.delete(TABLE, TEL+"=?", whereArgs);
        return cnt;
    }

    public int deleteById(int _id){
        int cnt=0;
        //根据指定的id号删除，返回值是被删除条目的个数
        String[] whereArgs=new String[]{ ""+_id};
        cnt=database.delete(TABLE, ID+"=?", whereArgs);
        return cnt;
    }

    public void deleteAll(){
        database.delete(TABLE, null, null);
    }

    public List<String> queryTels(){ //此函数无用
        List<String> Tels=new ArrayList<String>();
        List<ContactBean> contactBeans=queryAll();
        for (int i = 0; i < contactBeans.size(); i++) {
            Tels.add(contactBeans.get(i).getTel());
        }
        return Tels;
    }

    public List<ContactBean> queryAll(){
        Cursor cursor=null;
        //查询所有的数据
        //cursor=
        cursor=database.query(TABLE,null,null,null,null,null,null);
        return convertToContactBean(cursor);
    }

    public List<ContactBean> convertToContactBean(Cursor cursor){
        List<ContactBean> contactBeans=new ArrayList<ContactBean>();

        //根据cursor指向的数据集，得到一个list的列表
        if(cursor!=null) {
            int cnt = cursor.getCount();
            cursor.moveToFirst();
            for (int i = 0; i < cnt; i++) {
                ContactBean contactBean = new ContactBean();
                contactBean.setName(cursor.getString(cursor.getColumnIndex(NAME)));
                contactBean.setTel(cursor.getString(cursor.getColumnIndex(TEL)));
                contactBean.setType(cursor.getString(cursor.getColumnIndex(TYPE)));
                contactBean.set_id(cursor.getInt(cursor.getColumnIndex(ID)));
                contactBean.setGro(cursor.getString(cursor.getColumnIndex(GRO)));
                contactBeans.add(contactBean);
                cursor.moveToNext();
            }
        }
        return contactBeans;
    }

    public class OpenHelper extends SQLiteOpenHelper {

        public OpenHelper(Context context) {
            super(context, DBNAME, null, VERSON);
            // TODO Auto-generated constructor stub
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // TODO Auto-generated method stub
            String sql = "create table contact( _id integer primary key autoincrement, name varchar(64), tel varchar(64), type varchar(64), gro varchar(64))";
            db.execSQL(sql);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub

        }

    }
}

